<?php

namespace App\Http\Controllers\admin_system;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class CountController extends Controller
{

    public function enter(Request $request)
    {
        $action     = $request->input('action');
        if(method_exists($this,$action))
        {
            return $this->$action($request);
        }
        return "action is not exists";
    }


    /**
     * 书籍阅读统计
     *
     * */
    private function books(Request $request)
    {
        //获得最新的10本书
        $books = DB::table('bookinfo')
            ->select('BookTitle','BookID')
            ->where('DeleteMark',1)
            ->orderBy('BookID','DESC')
            ->paginate(5);
        $bookArr    = [];
        $reads      = [];
        $readTimes  = [];
        $readPlans  = [];

        foreach($books as $book)
        {
            array_push($bookArr,'"'.$book->BookTitle.'"');
            //获取观看人数
            $SQL            = "SELECT count(DISTINCT(user_id)) AS total FROM media_play_log WHERE media_id = $book->BookID AND (media_type = \"lhddmp4\" OR media_type = \"lhddmp3\")";
            $info           = DB::select($SQL);
            array_push($reads,$info[0]->total);

            //观看总时长
            $SQL            = "SELECT SUM(UNIX_TIMESTAMP(end_time) - UNIX_TIMESTAMP(begin_time)) totalTime FROM media_play_log WHERE media_id = $book->BookID AND (media_type = \"lhddmp4\" OR media_type = \"lhddmp3\")";
            $info           = DB::select($SQL);
            array_push($readTimes,ceil($info[0]->totalTime/3600));

            //填写改进计划
            $SQL            = "SELECT COUNT(SumUpID) AS total FROM booksumup WHERE BookID = $book->BookID";
            $info           = DB::select($SQL);
            array_push($readPlans,$info[0]->total);
        }

        $data = [
            'books' => implode(',',$bookArr),
            'reads' => implode(',',$reads),
            'times' => implode(',',$readTimes),
            'plans' => implode(',',$readPlans)
        ];

        return view('admin_system/count/books',['data'=>$data,'books'=>$books]);

    }


    /**
     * 社群学习情况
     * */
    private function readparty(Request $request)
    {
        //获得所有读书会
        $readPartys     = DB::table('read_party')->where('ReaParType',1)->get();
        $books          = DB::table('bookinfo')
            ->where('video_type','lhld')
            ->where('IsShow',1)
            ->where('DeleteMark',1)
            ->orderBy('BookID','DESC')
            ->get();
        $data   = [
            'readPartys'=> $readPartys,
            'books'     => $books
        ];
        return view('admin_system/count/readparty',$data);
    }


    /**
     * 获得读书会的数据
     * */
    private function readparty_data(Request $request)
    {
        $readId     = $request->input('readparty',0);
        $bookId     = $request->input('book',0);
        $beginTime  = $request->input('beginDate',0);
        $endTime    = $request->input('endDate',0);

        $sql = "SELECT b.YourName,k.BookTitle,d.BookReview1,d.BookReview2,d.BookReview3,d.BookReview4,d.AddDate
              FROM read_party_member a
              LEFT JOIN memberinfo b ON b.UserID   = a.user_id
              LEFT JOIN booksumup d  ON d.MemNumber= b.MemNumber
              LEFT JOIN bookinfo  k  ON k.BookID  = d.BookID
              WHERE a.read_party_id = $readId
              AND a.is_delete = 0
              AND b.DeleteMark= 1 ";

        if($bookId > 0)
        {
            $sql .= " AND d.BookID = $bookId ";
        }
        if($beginTime != 0)
        {
            $sql = "AND d.AddDate >= '$beginTime' ";
        }

        if($endTime != 0)
        {
            $sql .= " AND d.AddDate <= '$endTime' ";
        }
        $members    = DB::select($sql);
        return view('admin_system/count/readparty-data',['members'=>$members]);
    }

}
